package com.deepglint.state;

import akka.stream.impl.ReducerState;
import com.deepglint.beans.SensorReading;
import com.sun.org.apache.bcel.internal.generic.RET;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.*;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author mj
 * @version 1.0
 * @date 2021-11-22 21:59
 */
public class StateTest_KeyedState {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
//        env.setStreamTimeCharacteristic(); // 1.12开始，默认的就是eventTime
        env.getConfig().setAutoWatermarkInterval(200);// 设置周期性的waterMark的周期时间

        DataStream<String> streamSource = env.socketTextStream("192.168.150.128", 7777);

        DataStream<SensorReading> dataStream = streamSource.map(line -> {
            String[] split = line.split(",");
            return new SensorReading(split[0], split[1], new Long(split[2]), new Double(split[3]));
        });

        SingleOutputStreamOperator<Integer> mapStream = dataStream.keyBy("id").map(new MyRichMapFunction());

        mapStream.print();

        env.execute();
    }

    static class MyRichMapFunction extends RichMapFunction<SensorReading, Integer> {

        // 值状态
        private ValueState<Integer> keyCountState;

        //列表状态
        private ListState<String> myListState;

        // 映射状态
        private MapState<String, Double> myMapState;

        //聚合状态
        private ReducingState<SensorReading> myReduceState;

        @Override
        public void open(Configuration parameters) throws Exception {
            keyCountState = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("key-value", Integer.class, 0));

            myListState = getRuntimeContext().getListState(new ListStateDescriptor<String>("my_list", String.class));

            myMapState = getRuntimeContext().getMapState(new MapStateDescriptor<String, Double>("my_map", String.class, Double.class));

            myReduceState = getRuntimeContext().getReducingState(new ReducingStateDescriptor<SensorReading>("my_reduce", new ReduceFunction<SensorReading>() {
                @Override
                public SensorReading reduce(SensorReading value1, SensorReading value2) throws Exception {
                    return null;
                }
            }, SensorReading.class));
        }

        @Override
        public Integer map(SensorReading value) throws Exception {
            // value
            Integer count = keyCountState.value();
            ++count;
            keyCountState.update(count);

            // list state
            for (String s : myListState.get()) {
                System.out.println(s);
            }
            myListState.add("hello");

            // reduce state
            myReduceState.add(new SensorReading());
            myReduceState.clear(); // 清空所有的状态

            // map state
            myMapState.get("1");
            myMapState.clear();
            myMapState.put("2", 12.3);
            myMapState.remove("1");

            return count;
        }
    }
}
